package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.ShowCommonTabActivityCustom;
import com.eqdd.yiqidian.adapter.MyFragmentPagerAdapter;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.TabEntity;
import com.eqdd.yiqidian.bean.rebuild.CommonTabBean;
import com.eqdd.yiqidian.ui.mine.shenqing.fragment.MyCreateBaseFragment;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lv on 17-9-19.
 */

public class ShowCommonTabActivity extends BaseActivity {

    private ShowCommonTabActivityCustom dataBinding;
    private CommonTabBean commonTabBean;
    private ArrayList<Fragment> fragments;


    @Override
    public void initBinding() {
        commonTabBean = (CommonTabBean) getIntent().getSerializableExtra("bean");
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_show_commen_tab);
        initTopTitleBar(View.VISIBLE, commonTabBean.getTitle());
        if (commonTabBean.isRight()) {
            initTopRightImage(R.mipmap.add, v -> {
                startActivityForResult(commonTabBean.getActivity(), null, commonTabBean.getRequestCode());
            });
        }
    }

    @Override
    public void initData() {
        isRxbus = true;
    }

    @Override
    public void setView() {
        fragments = new ArrayList<>();
        List<String> titles = commonTabBean.getTitles();
        ArrayList<CustomTabEntity> customTabEntities = new ArrayList<>();
        for (int i = 0; i < titles.size(); i++) {
            TabEntity tabEntity = new TabEntity(titles.get(i), 0, 0);
            customTabEntities.add(tabEntity);
            fragments.add(getFragment(i));
        }

        dataBinding.viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), titles, fragments));
        dataBinding.commonTabLayout.setTabData(customTabEntities);
        dataBinding.commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                dataBinding.viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        dataBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                                                          @Override
                                                          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                                                          }

                                                          @Override
                                                          public void onPageSelected(int position) {
                                                              dataBinding.commonTabLayout.setCurrentTab(position);
                                                          }

                                                          @Override
                                                          public void onPageScrollStateChanged(int state) {

                                                          }
                                                      }
        );
    }

    private Fragment getFragment(int status) {
        MyCreateBaseFragment fragment = new MyCreateBaseFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(Config.STATUS, status);
        bundle.putSerializable("bean", commonTabBean);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        fragments.get(dataBinding.viewPager.getCurrentItem()).onActivityResult(requestCode, resultCode, data);
    }
}
